<?php
/**
 * Created by mww
 * User: mww
 */

namespace app\admin\controller;
use think\Db;
use think\Request;

class Product extends Base
{
    /**
     * 首页
     */
    public function index(){
        if(request()->isAjax()){
            extract(input());
            $where = [
                'cp_status' => ['<>',3]
            ];
            if(session("company_id") > 0){
                $where['cp_c_id'] = session("company_id");
            }
            if(isset($user_name) && $user_name != ''){
                $where['cp_name'] = ['like', '%' . $user_name . '%'];
            }
            if(isset($cp_status) && $cp_status != ''){
                $where['cp_status'] = $cp_status;
            }
            if(isset($cp_c_id) && $cp_c_id != ''){
                $where['cp_c_id'] = $cp_c_id;
            }
            //注册时间查询
            if(isset($start) && $start != "" && isset($end) && $end == "")
            {
                $where['cp_time'] = ['>=',$start];
            }
            if(isset($end) && $end != "" && isset($start) && $start == "")
            {
                $where['cp_time'] = ['<=',$end];
            }
            if(isset($start) && $start != "" && isset($end) && $end != "")
            {
                $where['cp_time'] = ['between',[$start,$end]];
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $field = 'cp.*,c.c_name,user_username,user_phone';
            $count = Db::table('up_company_product')->alias('cp')
                ->join('up_company c','cp.cp_c_id = c.c_id','left')
                ->join('up_u_user u','cp.cp_charge_uid = u.user_id','left')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::table('up_company_product')->alias('cp')
                ->join('up_company c','cp.cp_c_id = c.c_id','left')
                ->join('up_u_user u','cp.cp_charge_uid = u.user_id','left')
                ->where($where)
                ->page($NowPage,$limits)
                ->order('cp_status asc,cp_update desc')
                ->field($field)
                ->select();
            if($lists){
                foreach ($lists as $k=>$v){
                    $lists[$k]['datas'] = Db::table('up_company_product_data')
                        ->where('cpd_cp_id',$v['cp_id'])
                        ->where('cpd_del',1)
                        ->count();
                }
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $coms = Db::table('up_company')
            ->where('c_verify',2)
            ->where('c_status','<>',3)
            ->field('c_id,c_name')
            ->select();
        $this->assign('coms',$coms);
        return $this->fetch();
    }


    /**
     * 冻结
     * @return \think\response\Json
     */
    public function down(){
        $cp_id = input('id');
        $res = Db::table('up_company_product')->where('cp_id',$cp_id)->setField('cp_status',2);
        if($res){
            return json(['code' => 200, 'msg' => '冻结成功']);
        }else{
            return json(['code' => 100, 'msg' => '冻结失败']);
        }
    }
    /**
     * 启用
     * @return \think\response\Json
     */
    public function up(){
        $cp_id = input('id');
        $res = Db::table('up_company_product')->where('cp_id',$cp_id)->setField('cp_status',1);
        if($res){
            return json(['code' => 200, 'msg' => '启用成功']);
        }else{
            return json(['code' => 100, 'msg' => '启用失败']);
        }
    }
    /**
     * 删除
     * @return \think\response\Json
     */
    public function cpDel(){
        $cp_id = input('id');
        $res = Db::table('up_company_product')->where('cp_id',$cp_id)->setField('cp_status',3);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    public function getUsers()
    {
        $cid = input('param.cid');
        $data = Db::table('up_u_user')
            ->where('user_hospital',$cid)
            ->where('user_status',1)
            ->where('user_verify',2)
            ->field('user_id,user_username,user_phone')
            ->order('user_auth desc')
            ->select();
        return json(['code'=>200,'msg'=>$data]);
    }
    public function getCitysMuli()
    {
        $id = input('param.id');
        $area = Db::name('area')
            ->where('pid',$id)
            ->field('district_id value,district name')
            ->select();
        return json(['code'=>200,'msg'=>$area]);
    }
    /**
     * 添加
     * @return mixed|\think\response\Json
     */
    public function addProduct(){
        if(Request::instance()->isPost()){
            extract(input());
            $today = date('Y-m-d H:i:s');
            $insert = [
                'cp_c_id' => $cp_c_id,
                'cp_charge_uid' => 0,
                'cp_browse' => $cp_browse,
                'cp_name' => $cp_name,
                'cp_thumb' => $cp_thumb,
                'cp_time' => $today,
                'cp_update' => $today,
            ];
            $res = Db::table('up_company_product')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $where = [
            'c_verify' => 2,
            'c_status' => 1
        ];
        if(session("company_id") > 0){
            $where['c_id'] = session("company_id");
        }
        $coms = Db::table('up_company')
            ->where($where)
            ->field('c_id,c_name')
            ->select();
        $this->assign('coms',$coms);
        return $this->fetch('product/add');
    }
    /**
     * 编辑
     * @return mixed|\think\response\Json
     */
    public function cpEdit(){
        if(Request::instance()->isPost()){
            extract(input());
            $today = date('Y-m-d H:i:s');
            $update = [
                'cp_c_id' => $cp_c_id,
                'cp_browse' => $cp_browse,
                'cp_name' => $cp_name,
                'cp_thumb' => $cp_thumb,
                'cp_update' => $today,
            ];
            $res = Db::table('up_company_product')->where('cp_id', $cp_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $cp_id = input('cp_id');
        $info = Db::table('up_company_product')
            ->where('cp_id',$cp_id)
            ->find();
        //
        $where = [
            'c_verify' => 2,
            'c_status' => 1
        ];
        if(session("company_id") > 0){
            $where['c_id'] = session("company_id");
        }
        $coms = Db::table('up_company')
            ->where($where)
            ->field('c_id,c_name')
            ->select();
        $this->assign([
            'info' => $info,
            'coms' => $coms,
        ]);
        return $this->fetch('product/edit');
    }
    //-------------------------------------资料-------------------------------------------
    /**
     * 查看资料
     * @return mixed|\think\response\Json
     */
    public function seeDatas(){
        if(request()->isAjax()){
            extract(input());
            $where['cpd_cp_id'] = session('cp_id');
            $where['cpd_del'] = 1;
            if(isset($user_name) && $user_name != ''){
                $where['cpd_name'] = ['like', '%' . $user_name . '%'];
            }
            if(isset($cpd_type) && $cpd_type != ''){
                $where['cpd_type'] = $cpd_type;
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_company_product_data')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::table('up_company_product_data')
                ->where($where)
                ->page($NowPage,$limits)
                ->order('cpd_top desc,cpd_sort desc,cpd_update desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $cp_id = input('cp_id');
        session('cp_id', $cp_id);
        return $this->fetch('product/datas');
    }
    /**
     * 设置排序值
     * @return mixed|\think\response\Json
     */
    public function updateSort(){
        $id = input('id');
        $param = [
            'cpd_sort'=>input('value'),
            'cpd_update'=>date('Y-m-d H:i:s'),
        ];
        $res = Db::name('company_product_data')->where('cpd_id',$id)->update($param);
        if($res){
            return json(['code' => 200, 'msg' => '修改成功']);
        }else{
            return json(['code' => 100, 'msg' =>' 修改失败']);
        }
    }
    /**
     * 改置顶状态
     * @author
     */
    public function changeTop()
    {
        extract(input());
        $res = Db::table('up_company_product_data')
            ->where('cpd_id',$id)
            ->update([
                'cpd_update' => date('Y-m-d H:i:s'),
                'cpd_top' => $num
            ]);
        if($res){
            return json(['code' => 200, 'msg' => '成功']);
        }else{
            return json(['code' => 100, 'msg' => '失败']);
        }
    }

    /**
     * 删除资料
     * @return \think\response\Json
     */
    public function dataDel(){
        $ct_id = input('id');
        $res = Db::table('up_company_product_data')->where('cpd_id',$ct_id)->setField('cpd_del',2);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    /**
     * 添加资料
     * @return mixed|\think\response\Json
     */
    public function addData(){
        if(Request::instance()->isPost()){
            extract(input());
            $today = date('Y-m-d H:i:s');
            if(!$cpd_sheng) {
                $cpd_sheng = 0;
                $cpd_shi = 0;
            }else{
                $shengArr = Db::table('up_area')
                    ->field('district_id value,district name')
                    ->where('district_id','in',$cpd_sheng)
                    ->group('pid')
                    ->column('pid');
                $cpd_shi = ',' . $cpd_sheng . ',';
                $cpd_sheng = implode($shengArr,',');
            }
            $insert = [
                'cpd_cp_id' => session('cp_id'),
                'cpd_name' => $cpd_name,
                'cpd_type' => $cpd_type,
                'cpd_thumb' => $cpd_thumb,
                'cpd_browse' => $cpd_browse,
                'cpd_sheng' => $cpd_sheng,
                'cpd_shi' => $cpd_shi,
                'cpd_sort' => $cpd_sort,
                'cpd_time' => $today,
                'cpd_update' => $today,
            ];
            //内容
            if($cpd_type == 1){
                if(!$cpd_content1){
                    return json(['code' => 100, 'msg' => '图片不能为空']);
                }
                $insert['cpd_content'] = $cpd_content1;
            }elseif ($cpd_type == 2){
                if(!$cpd_content2){
                    return json(['code' => 100, 'msg' => '文档不能为空']);
                }
                $insert['cpd_content'] = $cpd_content2;
                $pdfToImg = new \app\admin\controller\Upload;
                $imgs = $pdfToImg->toImgUploadQn($cpd_content2);
                $insert['cpd_pdf_img'] = $imgs;
            }else{
                if(!$cpd_content3){
                    return json(['code' => 100, 'msg' => '视频不能为空']);
                }
                $insert['cpd_content'] = $cpd_content3;
            }
            //缩略图
            if(!$cpd_thumb){
                if($cpd_type == 1){
                    $thumb_arr = explode(',',$cpd_content1);
                    $insert['cpd_thumb'] = $thumb_arr[0];
                }elseif ($cpd_type == 2){
                    $insert['cpd_thumb'] = config('pdfurl')['url'];
                }else{
                    $insert['cpd_thumb'] = $cpd_content3 . '?ci-process=snapshot&time=1';
                }
            }
            $res = Db::table('up_company_product_data')->insertGetId($insert);
            if($res){
                $cp_name = Db::table('up_company_product')->where('cp_id',session('cp_id'))
                    ->field('cp_name,cp_c_id,cp_charge_uid')->find();
                $c_name = Db::table('up_company')->where('c_id',$cp_name['cp_c_id'])
                    ->value('c_name');
                //
                $content = '平台发布针对'.$c_name.'企业的'.$cp_name['cp_name'].'产品，发布了'.$cpd_name.'资料，请知悉：';
                allInNotice('资料发布提醒',$content,4,$cp_name['cp_charge_uid'],$res);
                try {
                    $this->Jpush(1, $cp_name['cp_charge_uid'], '您有新的产品资料发布', ['id' => session('cp_id'),'n_type' => 6]);
                } catch (\Exception $e) {
                }
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $province = Db::table('up_area')
            ->field('district_id value,district name')
            ->where('pid',0)
            ->select();
        foreach ($province as $k => $v){
            $province[$k]['children'] = Db::name('area')
                ->where('pid',$v['value'])
                ->field('district_id value,district name')
                ->select();
        }
        $this->assign('province',json_encode($province));
        return $this->fetch('product/add_data');
    }
    /**
     * 编辑资料
     * @return mixed|\think\response\Json
     */
    public function editData(){
        if(Request::instance()->isPost()){
            extract(input());
            $today = date('Y-m-d H:i:s');
            if(!$cpd_sheng) {
                $cpd_sheng = 0;
                $cpd_shi = 0;
            }else{
                $shengArr = Db::table('up_area')
                    ->field('district_id value,district name')
                    ->where('district_id','in',$cpd_sheng)
                    ->group('pid')
                    ->column('pid');
                $cpd_shi = ',' . $cpd_sheng . ',';
                $cpd_sheng = implode($shengArr,',');
            }
            $update = [
                'cpd_name' => $cpd_name,
                'cpd_type' => $cpd_type,
                'cpd_thumb' => $cpd_thumb,
                'cpd_browse' => $cpd_browse,
                'cpd_sheng' => $cpd_sheng,
                'cpd_shi' => $cpd_shi,
                'cpd_sort' => $cpd_sort,
                'cpd_update' => $today,
            ];
            //内容
            if($cpd_type == 1){
                if(!$cpd_content1){
                    return json(['code' => 100, 'msg' => '图片不能为空']);
                }
                $update['cpd_content'] = $cpd_content1;
            }elseif ($cpd_type == 2){
                if(!$cpd_content2){
                    return json(['code' => 100, 'msg' => '文档不能为空']);
                }
                $update['cpd_content'] = $cpd_content2;
                $old = Db::table('up_company_product_data')->where('cpd_id', $cpd_id)->value('cpd_content');
                if($old != $cpd_content2){
                    $pdfToImg = new \app\admin\controller\Upload;
                    $imgs = $pdfToImg->toImgUploadQn($cpd_content2);
                    $update['cpd_pdf_img'] = $imgs;
                }
            }else{
                if(!$cpd_content3){
                    return json(['code' => 100, 'msg' => '视频不能为空']);
                }
                $update['cpd_content'] = $cpd_content3;
            }
            //缩略图
            if(!$cpd_thumb){
                if($cpd_type == 1){
                    $thumb_arr = explode(',',$cpd_content1);
                    $update['cpd_thumb'] = $thumb_arr[0];
                }elseif ($cpd_type == 2){
                    $update['cpd_thumb'] = config('pdfurl')['url'];
                }else{
                    $update['cpd_thumb'] = $cpd_content3 . '?ci-process=snapshot&time=1';
                }
            }
            $res = Db::table('up_company_product_data')->where('cpd_id', $cpd_id)->update($update);
            if($res){
                $this->editLikes(1,$cpd_id);
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $cpd_id = input('cpd_id');
        $info = Db::table('up_company_product_data')
            ->where('cpd_id',$cpd_id)
            ->find();
        if($info['cpd_sheng'] != 0){
            $info['cpd_shi'] = trim($info['cpd_shi'],',');
            $info['cpd_shi'] = explode(',',$info['cpd_shi']);
        }
        //------------------
        $province = Db::table('up_area')
            ->field('district_id value,district name')
            ->where('pid',0)
            ->select();
        foreach ($province as $k => $v){
            $sons = Db::name('area')
                ->where('pid',$v['value'])
                ->field('district_id value,district name')
                ->select();
            if($info['cpd_sheng'] != 0){
                foreach ($sons as $k2 => $v2){
                    if(in_array($v2['value'],$info['cpd_shi'])) $sons[$k2]['selected'] = true;
                }
            }
            $province[$k]['children'] = $sons;
        }
        $this->assign('province',json_encode($province));
        $this->assign([
            'info' => $info
        ]);
        return $this->fetch('product/edit_data');
    }
    /**
     * 资料详情
     * @return mixed|\think\response\Json
     */
    public function dataInfo(){
        $cpd_id = input('cpd_id');
        $info = Db::table('up_company_product_data')->alias('cpd')
            ->where('cpd_id',$cpd_id)
            ->find();
        //------------------
        if($info['cpd_type'] == 1){
            if($info['cpd_content']){
                $info['cpd_content'] = explode(',',$info['cpd_content']);
            }else{
                $info['cpd_content'] = [];
            }
        }
        if($info['cpd_sheng']){
            $info['cpd_shi'] = trim($info['cpd_shi'],',');
            $shis = Db::table('up_area')->where('district_id','in',$info['cpd_shi'])->column('district');
            $info['shi'] = implode($shis,',');
            $shengs = Db::table('up_area')->where('district_id','in',$info['cpd_sheng'])->column('district');
            $info['sheng'] = implode($shengs,'/');
        }
        $this->assign([
            'info' => $info
        ]);
        return $this->fetch('product/data_info');
    }
}